package com.example.zhu.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 取件码正则
 */
public class RegularExpression {

    /**
     * 01~41-0~9-0~9*4
     *
     * @return
     */
    public static boolean in(String takeCode) {

        if (takeCode == null) {
            return false;
        }
        String regex = "-";
        String[] split = takeCode.split(regex);
        if (split.length != 3) {
            return false;
        }
        String regex1 = "^([0-3][0-9]|4[0-1]|[0-9])$";
        if (!split[0].matches(regex1)) {
            return false;
        }
        String regex2 = "^\\d+$";
        if (!split[1].matches(regex2)) {
            return false;
        }
        String regex3 = "^\\d{4}$";
        Pattern pattern = Pattern.compile(regex3);
        Matcher m = pattern.matcher(split[2]);
        if (!m.matches()) {
            return false;
        }

//        String res = "^([0-3][0-9]|4[0-1])-\\d-\\d{4}$";
//        boolean matches = takeCode.matches(res);
//        return matches;

        return true;
    }


    /**
     * 66~74-dd-0~9*4
     */
    public static boolean out(String takeCode) {
        String regex = "^(6[6-9]|7[0-4])-(\\d+)-\\d{4}$";
        Pattern pattern = Pattern.compile(regex);
        Matcher m = pattern.matcher(takeCode);
        return m.matches();
    }

    /**
     * A1**-0~9*4
     * @param takeCode
     * @return
     */
    public static boolean sf(String takeCode) {
        String regex = "^A...-\\d{4}$";
        Pattern pattern = Pattern.compile(regex);
        Matcher m = pattern.matcher(takeCode);
        return m.matches();
    }

    /**
     * 0~14号柜-0~9*8
     */
    public static boolean cabinet(String takeCode){
        String regex = "^(\\d|1[0-4])-\\d{8}$";
        Pattern pattern = Pattern.compile(regex);
        Matcher m = pattern.matcher(takeCode);
        return m.matches();
    }

    /**
     * 88-0~7-0~9*4
     * @param takeCode
     * @return
     */
    public static boolean pdd(String takeCode){
        String regex = "^88-(\\d+)-\\d{4}$";
        Pattern pattern = Pattern.compile(regex);
        Matcher m = pattern.matcher(takeCode);
        return m.matches();
    }

    /**
     * 整体校验
     * @param takeCode
     * @return
     */
    public static boolean verify(String takeCode){

        if(in(takeCode)||out(takeCode)||sf(takeCode)||cabinet(takeCode)||pdd(takeCode)){
            return true;
        }else {
            return false;
        }

    }


    public static boolean verify_(String takeCode){

        if(kf(takeCode)||yd8(takeCode)||s3d41(takeCode)||s2d38(takeCode)||sf(takeCode)||cabinet(takeCode)||pdd(takeCode)||jd(takeCode)){
            return true;
        }else {
            return false;
        }

    }


    public static boolean jd(String takeCode){
        String regex = "^J-(\\d)-(\\w+)$";
        Pattern pattern = Pattern.compile(regex);
        Matcher m = pattern.matcher(takeCode);
        return m.matches();
    }

    public static boolean yd8(String takeCode){
        String regex = "^([1-8]|50)-(\\d+)-\\d{4}$";
        Pattern pattern = Pattern.compile(regex);
        Matcher m = pattern.matcher(takeCode);
        return m.matches();
    }

    public static boolean s3d41(String takecode){
        String regex = "^([1][3579]|[23][13579]|41)-(\\d+)-\\d{4}$";
        Pattern pattern = Pattern.compile(regex);
        Matcher m = pattern.matcher(takecode);
        return m.matches();
    }

    public static boolean s2d38(String takecode){
        String regex = "^([1][2468]|[23][02468])-(\\d+)-\\d{4}$";
        Pattern pattern = Pattern.compile(regex);
        Matcher m = pattern.matcher(takecode);
        return m.matches();
    }

    public static boolean kf(String takecode){
        String regex = "^kf-(.+)$";
        Pattern pattern = Pattern.compile(regex);
        Matcher m = pattern.matcher(takecode);
        return m.matches();
    }

}
